//
//  MKCategoryViewController.m
//  ManKeCartoon
//
//  Created by dllo on 15/10/20.
//  Copyright © 2015年 ManKe. All rights reserved.
//

#import "MKCategoryViewController.h"
#import "MKBaseHeader.h"

@interface MKCategoryViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation MKCategoryViewController
- (void)viewWillAppear:(BOOL)animated
{
    
    self.tabBarController.tabBar.hidden = YES;
    //    self.navBarView.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
//    self.navBarView.hidden = NO;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, 24, 100, 30)];
    
    [self.navBarView addSubview:titleLabel];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = self.titleName;
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    
    _categorTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 60*HEIGHT, 375*WIDTH, 593*HEIGHT)];
    
    _categorTable.dataSource =self;
    
    _categorTable.delegate = self;
    
    _categorTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_categorTable];
    
    
    
    [self setUpTableView];
    [self addfooter];
    [self AFNDataNumber:15];
    
    [self setUpBackButton];

    
   
    
//     [self  categorTable];
    
    
    // Do any additional setup after loading the view.
}


#pragma mark - 创建一个返回按钮
- (void)setUpBackButton
{
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setFrame:CGRectMake(10, 18, 30, 30)];
    [_backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.navBarView addSubview:_backButton];
}
#pragma mark - 返回按钮点击方法
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}



-(void)setUpTableView
{
    __block MKCategoryViewController *vc = self;
    
   
  [self.categorTable addLegendHeaderWithRefreshingBlock:^{
      vc.isUpLoading = YES;
      vc.page = 15;
      [vc AFNDataNumber:15];
  }];
    
    [_categorTable.header beginRefreshing];
//    [MKAFNmodel getdataWithString:[NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Classify/comicClassifyDetail/classify_id/%@/sort_type/2/page/1/listcnt/15/user_local_version/4.8.7/",_classify_id] block:^(id block) {
//        
//    }];

//    [_categorTable.header beginRefreshing];

}

-(void)addfooter
{
    __block MKCategoryViewController *vc = self;
    [self.categorTable addLegendFooterWithRefreshingBlock:^{
        vc.isUpLoading = YES;
        vc.page +=15;
        [vc AFNDataNumber:vc.page];
    }];
}

-(void)AFNDataNumber:(NSInteger)Number;
{

    
        [MKAFNmodel getdataWithString:[NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Classify/comicClassifyDetail/classify_id/%@/sort_type/2/page/1/listcnt/%ld/user_local_version/4.8.7/",_classify_id,_page] block:^(id block) {
            
            self.categorArray = [[NSMutableArray alloc]init];
            if (self.isUpLoading == NO) {
                [self.categorArray removeAllObjects];
            }
            for (NSDictionary *dic in [block objectForKey:@"data"]) {
                
                MKChartsModel *model = [[MKChartsModel alloc]init];
                
                [model setValuesForKeysWithDictionary:dic];
                
                [self.categorArray addObject:model];
                
                
                [self.categorTable .header endRefreshing];
                //            NSLog(@"asdsad%@",self.categorArray);
            }
           
            [_categorTable reloadData];
        }];
    

    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MKCartoonViewController *cartoonView = [[MKCartoonViewController alloc]init];
    MKChartsModel *model = self.categorArray[indexPath.row];
    cartoonView.comic_id = model.comic_id;
    [self.navigationController pushViewController:cartoonView animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return _categorArray.count ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *cellID = @"cell";
    MKClassifyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[MKClassifyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    MKChartsModel *model = self.categorArray[indexPath.row];
    
    
//    NSLog(@"11111%@",model);
    
    [cell getCell:model row:indexPath.row];
    
    
    
    return cell;
    }


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150*HEIGHT;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
